package receiver;

import android.content.Context;
import android.content.Intent;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.lidroid.xutils.HttpUtils;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import application.UIApplication;

/**
 * Created by yanglei on 16/7/1.
 */
public class aliYunPushReceiver extends MessageReceiver {
    public static int notifId=0;
    private HttpUtils http = new HttpUtils(UIApplication.NetWorkTimeOut);

    @Override
    public void onHandleCall(final Context context, Intent intent) {
        String str = intent.getStringExtra("body");
        try {
            intent.putExtra("body",java.net.URLDecoder.decode(str, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        super.onHandleCall(context, intent);



//        if(StringUtil.isEmpty(str)) {
//            LogUtils.e("json body is Empty!");
//        } else {
//            try{
//                JSONObject jsonObject = new JSONObject(str);
//                String content = jsonObject.isNull("content")?null:jsonObject.getString("content");
//                content = java.net.URLDecoder.decode(content, "UTF-8");
//                String title = jsonObject.isNull("title")?null:jsonObject.getString("title");
//                JSONObject ext = jsonObject.isNull("ext")?null:jsonObject.getJSONObject("ext");
//
//                try {
//                    final String deTitle = context.getResources().getString(R.string.app_name)+" 有条新消息";
//                    final String deMsg = content;
//
////                    try
////                    {
////                        JSONObject ALIJsonObj = new JSONObject(deMsg);
////                        if(ALIJsonObj.getString("action").equalsIgnoreCase("GetData")){
////
////                            RequestParams params = new RequestParams();
////                            params.addQueryStringParameter("__session__", AppApplication.getSession());
////                            http.send(HttpMethod.GET, HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.GetMobileNotificationMessage), params, new RequestCallBack() {
////                                @Override
////                                public void onSuccess(ResponseInfo responseInfo) {
////                                    String re = responseInfo.result.toString();
////
////                                    try
////                                    {
////                                        JSONObject jsonObj = new JSONObject(re);
////                                        if(jsonObj.getBoolean("Succeed"))
////                                        {
////                                            JSONArray dataArry = jsonObj.getJSONArray("Data");
////                                            if(dataArry!=null)
////                                            {
////                                                for (int i=0;i<dataArry.length();i++)
////                                                {
////                                                    JSONObject item = dataArry.getJSONObject(i);
////                                                    String action = item.isNull("action")?"":item.getString("action");
////                                                    String title = item.isNull("title")?"":item.getString("title");
////                                                    String content = item.isNull("content")?"":item.getString("content");
////                                                    String data = item.isNull("data")?"":item.getString("data");
////
////                                                    //
////                                                    notifId = notifId + 1;
////                                                    if (notifId > 1500) {
////                                                        notifId = 0;
////                                                    }
////                                                    Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
////                                                    NotificationUtil.notification(context, notifId, R.mipmap.icon_small58, largeIcon,
////                                                            deTitle, deTitle, content,
////                                                            null, "app.SplashScreen");
////                                                }
////                                            }
////
////                                        }
////                                    }
////                                    catch (Exception e){
////                                        LogUtils.e(""+e.getMessage());
////                                    }
////
////
////
////                                }
////
////                                @Override
////                                public void onFailure(HttpException e, String s) {
////                                    LogUtils.e(s + "");
////                                }
////                            });
////                        }
////                        else{
////                            super.onHandleCall(context, intent);
////                        }
////                    }
////                    catch (Exception e)
////                    {
////                        //
////                        super.onHandleCall(context, intent);
////                        LogUtils.e(""+e.getMessage());
////                    }
//
//                }
//                catch (Exception e){
//                    LogUtils.e(""+e.getMessage());
//                }
//            }
//            catch (Exception e)
//            {
//                LogUtils.e(""+e.getMessage());
//            }
//        }


    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }

    @Override
    protected void onMessage(final Context context, final CPushMessage cPushMessage) {
        super.onMessage(context, cPushMessage);
    }

    @Override
    protected void onNotification(final Context context, String title, String text, Map<String, String> map) {
        super.onNotification(context, title, text, map);
    }

    @Override
    protected void onNotificationOpened(Context context, String title, String text, String mapJson) {
        super.onNotificationOpened(context, title, text, mapJson);
    }

    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        super.onNotificationRemoved(context, messageId);
    }
}
